<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style type="text/css">
      .container .cart {
        width: 400px;
        /*background-color: lightgreen;*/
        margin: auto;
      }
      .container .title {
        background-color: lightblue;
        height: 40px;
        line-height: 40px;
        text-align: center;
        /*color: #fff;*/
      }
      .container .total {
        background-color: #ffce46;
        height: 50px;
        line-height: 50px;
        text-align: right;
      }
      .container .total button {
        margin: 0 10px;
        background-color: #dc4c40;
        height: 35px;
        width: 80px;
        border: 0;
      }
      .container .total span {
        color: red;
        font-weight: bold;
      }
      .container .item {
        height: 55px;
        line-height: 55px;
        position: relative;
        border-top: 1px solid #add8e6;
      }
      .container .item img {
        width: 45px;
        height: 45px;
        margin: 5px;
      }
      .container .item .name {
        position: absolute;
        width: 130px;
        top: 0;
        left: 55px;
        font-size: 14px;
      }

      .container .item .change {
        width: 100px;
        position: absolute;
        top: 0;
        right: 50px;
      }
      .container .item .change a {
        font-size: 20px;
        width: 30px;
        text-decoration: none;
        background-color: lightgray;
        vertical-align: middle;
      }
      .container .item .change .num {
        width: 40px;
        height: 25px;
      }
      .container .item .del {
        position: absolute;
        top: 0;
        right: 0px;
        width: 40px;
        text-align: center;
        font-size: 40px;
        cursor: pointer;
        color: red;
      }
      .container .item .del:hover {
        background-color: orange;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="container">
        <div class="cart">
          <cart-title></cart-title>
          <cart-item></cart-item>
          <cart-total></cart-total>
        </div>
      </div>
    </div>
    <script type="text/javascript" src="vue.js"></script>
    <script type="text/javascript">
      const hub = new Vue();
      Vue.component("cart-item", {
        template: `<div>
          <div class="item" v-for="item in arr" :key="item.id">
            <img :src="item.img"/>
            <div class="name">{{item.name}} | 价格：{{item.num*item.price}}</div>
            <div class="change">
              <a href="" @click.prevent="change(item.id,$event,'sub')">－</a>
              <input type="text" class="num" :value="item.num" @blur='change(item.id,$event,"inp")' />
              <a href="" @click.prevent='change(item.id,$event,"add")'>＋</a>
            </div>
            <div class="del" @click=del(item.id)>×</div>
          </div>
          </div>`,
        data() {
          return {
            arr: [
              { id: 1, name: "手机", img: "img/a.jpg", price: 2000, num: 1 },
              { id: 2, name: "电脑", img: "img/b.jpg", price: 1000, num: 1 },
              { id: 3, name: "空调", img: "img/c.jpg", price: 1000, num: 1 },
              { id: 4, name: "汽车", img: "img/d.jpg", price: 1000, num: 1 },
              { id: 5, name: "玩具", img: "img/e.jpg", price: 1000, num: 1 },
            ],
          };
        },
        mounted() {
          console.log(123);
          this.trigger()
        },
        methods: {
          trigger(){
            hub.$emit(
              "change-total",
              this.arr.map((item) => item.num * item.price)
            );
          },
          change(id, e, type) {
            if (type == "inp") {
              this.arr.forEach((item) => {
                if (item.id == id) {
                  item.num = e.target.value;
                  return;
                }
              }); 
            } else if (type == "sub") {
              this.arr.forEach((item) => {
                if (item.id == id) {
                  if (item.num <= 1) {
                    return;
                  }
                  item.num -= 1;
                }
              });
            } else if (type == "add") {
              this.arr.forEach((item) => {
                if (item.id == id) {
                  item.num += 1;
                }
              });
            }
            this.trigger()
          },
          del(id){
            this.arr=this.arr.filter(item=>item.id!==id)
            this.trigger()
          }
        },
       
      });
      Vue.component("cart-title", {
        template: `<div class="title">我的商品</div>`,
        data() {
          return {};
        },
      });
      Vue.component("cart-total", {
        template: `<div class="total">
          <span>总价：{{res}}</span>
          <button>结算</button>
        </div>`,
        data() {
          return {
            res: "",
          };
        },
        created() {
          hub.$on("change-total", (val) => {
            let total = 0;
            val.forEach((item) => (total += item));
            this.res = total;
          });
         
        },
      });
      var vm = new Vue({
        el: "#app",
        data: {},
      });
    </script>
  </body>
</html>
